<template>
    <div>
        <el-form :model="model" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-row>
                <el-col :span="8">
                    <el-form-item label="订单编号" prop="WorkorderCode">
                        <el-input v-model="model.WorkorderCode"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="8">
                    <el-form-item label="工单名称" prop="WorkorderName">
                        <el-input v-model="model.WorkorderName"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="8">
                    <el-form-item label="订单来源" prop="OrderSource" label-width="120px">
                        <el-radio-group v-model="model.OrderSource">
                            <el-radio label="客户订单">客户订单</el-radio>
                            <el-radio label="库存备货">库存备货</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="8">
                    <el-form-item label="生产订单日期" prop="OrderDate">
                        <el-date-picker type="date" placeholder="选择日期" v-model="model.OrderDate"
                            style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-col>

                <el-col :span="8">
                    <el-form-item label="生产数量" prop="Quantity">
                        <el-input v-model="model.Quantity"></el-input>
                    </el-form-item>
                </el-col>


                <el-col :span="8">
                    <el-form-item label="批次号" prop="BatchCode">
                        <el-input v-model="model.BatchCode"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="生产日期" prop="ProduceDate">
                        <el-date-picker type="date" placeholder="选择日期" v-model="model.ProduceDate"
                            style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-col>

                <el-col :span="8">
                    <el-form-item label="需求日期" prop="RequestDate">
                        <el-date-picker type="date" placeholder="选择日期" v-model="model.RequestDate"
                            style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="备注" prop="Health">
                        <el-input v-model="model.Health"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-divider></el-divider>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="产品名称" prop="ProductName">
                        <el-input v-model="model.ProductName" :readonly="true" @focus="ProWorkorderBOW"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="产品编号" prop="ProductCode">
                        <el-input v-model="model.ProductCode" :readonly="true" @focus="ProWorkorderBOW"
                            :IsFirst="true"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="8">
                    <el-form-item label="单位" prop="UnitOfMeasure">
                        <el-input v-model="model.UnitOfMeasure" :readonly="true"></el-input>
                    </el-form-item>
                </el-col>

            </el-row>
 

            <el-row>
                <el-col :span="8">
                    <el-form-item label="规格型号" prop="ProductSpc">
                        <el-input v-model="model.ProductSpc" :readonly="true"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="model.OrderSource == '客户订单'">
                    <el-form-item label="客户编码" prop="ClientCode">
                        <el-input v-model="model.ClientCode"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="8" v-if="model.OrderSource == '客户订单'">
                    <el-form-item label="客户名称" prop="ClientName">
                        <el-input v-model="model.ClientName"></el-input>
                    </el-form-item>
                </el-col>

            </el-row>


           
            <el-form-item>
                <el-button type="primary" @click="submitForm">{{ isUpdate ? '立即保存' : '立即创建' }}</el-button>
                <el-button @click="resetForm">重置</el-button>
            </el-form-item>

            <div style="border: 1px solid #dcdfe6; padding: 10px;">
                <h1 style="margin: 10px;">BOM组成</h1>
                <el-table :data="tableData" ref="dataForm" v-loading="isLoading" height="360" >
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column prop="BomItemCode" label="物料编码" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="BomItemName" label="物料名称" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="BomItemSpec" label="规格" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="UnitOfMeasure" label="单位" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="ItemOrProduct" label="物料/产品" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="Quantity" label="使用比例" :show-overflow-tooltip="true"></el-table-column>
                </el-table>
            </div>
        </el-form>

        <el-dialog append-to-body :close-on-click-modal="false" :title="dialogDomain.title"
            :visible.sync="dialogDomain.dialogVisible" width="60%">
            <ProWorkorderBOW @callback="AddBomCallBack" :IsFirst="true" v-if="dialogDomain.dialogVisible" />
        </el-dialog>

    </div>
</template>

<script>
import ProWorkorderBOW from '@/views/Product/ProductView/components/ProductBomInput.vue'
export default {
    components: {
        ProWorkorderBOW
    },
    props: {
        model: {
            require: true,
            type: Object
        },
        isUpdate: {
            require: true,
            type: Boolean
        },
    },
    data() {
        return {

            ProProcessList: [],
            rules: {
                WorkorderCode: { required: true, message: '请输入订单编号', trigger: 'blur' },
                WorkorderName: { required: true, message: '请输入工单名称', trigger: 'blur' },
                ProductId: { required: true, message: '请选择产品'},
                Quantity: { required: true, message: '请输入生产数量', trigger: 'blur'},
                OrderSource: { required: true, message: '请选择订单来源', trigger: 'blur' },
                RequestDate: { required: true, message: '请选择需求日期', trigger: 'blur' },
                ProductCode: { required: true, message: '请选择产品编号' },
            },
            DepartmentList: [],
            SystemPostList: [],
            tableData: [],
            isLoading: false,
            dialogDomain: {
                title: '物料产品选择',
                dialogVisible: false,
            }
        }
    },
    created() {
        if(this.isUpdate){
            this.GetProductBombyId(this.model.ProductId);
        }
    },
    methods: {
        submitForm() {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {

                    if (this.isUpdate) {
                        this.put();
                    }
                    else {
                        this.add();
                    }

                } else {
                    this.showNotify('请输入必填项');
                    return false;
                }
            });
        },
        resetForm() {
            this.$refs['ruleForm'].resetFields();
        },
        add() {
            this.$http.postForm('/api/ProWorkorder', { model: this.model }, { loading: this.$loading() })
                .then(res => {
                    this.$emit('callback');
                })

        },
        //修改
        put() {
            this.$http.putForm('/api/ProWorkorder', { model: this.model }, { loading: this.$loading() })
                .then((res) => {
                    this.$emit('callback');
                })
        },
        ProWorkorderBOW() {
            this.dialogDomain.title = "选择产品分类";
            this.dialogDomain.dialogVisible = true;
        },
        AddBomCallBack(model) {
            this.GetProductBombyId(model.Id);
            this.model.ProductId = model.Id,
            this.model.ProductCode = model.ItemCode,
            this.model.ProductName = model.ItemName,
            this.model.ProductSpc = model.Spec,
            this.model.UnitOfMeasure = model.UnitOfMeasure,
            this.dialogDomain.dialogVisible = false;
        },
        GetProductBombyId(Id) {
            this.isLoading = true;
            this.$http.get('/api/Item/GetProductBombyId', {
                params: {
                    ItemId: Id
                }
            })
                .then(res => {
                    this.tableData = res.Data;
                    this.isLoading = false;
                })
        },

    }
}
</script>

<style></style>